package org.zxp.thread.ccontainer;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * @program: zxp-thread-test
 * @description:
 * @author: X-Pacific zhang
 * @create: 2019-08-14 09:50
 **/
public class SynchronizedListTest {
    public static void main(String[] args) throws InterruptedException {
//        List arr = new ArrayList();
//        CountDownLatch c = new CountDownLatch(10000);
//
//        for (int i = 0; i < 10000; i++) {
//            new Thread(()->{arr.add(1);c.countDown();}).start();
//        }
//        c.await();
//        System.out.println(arr.size());//9999



        List arr2 = new ArrayList();
        List safeArr = Collections.synchronizedList(arr2);
        CountDownLatch c2 = new CountDownLatch(10000);
        for (int i = 0; i < 10000; i++) {
            new Thread(()->{safeArr.add(1);c2.countDown();}).start();
        }
        c2.await();
        System.out.println(arr2.size());//10000
        System.out.println(safeArr.size());//10000

    }
}
